{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#   [Py4Hw User Guide](../UserGuide.ipynb)> 1.6 Wires\n",
    "###  [Next: 1.7 Interfaces](interfaces.ipynb)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Wires are required to interconnect the logic blocks instantiated in circuits.\n",
    "In py4hw wires are created by instantiating objects of the **Wire** class.\n",
    "\n",
    "Wire require the following parameters in their constructor\n",
    "\n",
    "1. The parent logic circuit. Every wire is created by a logic circuit. This does not has a strong implication but it is useful to track object creation.\n",
    "\n",
    "2. A name. All objects in py4hw have a name.\n",
    "\n",
    "3. A bit width. Wires can have a single bit, or an arbitrary number of bits. There is no limit to the number of bits.\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "tags": [
     "remove_input",
     "remove_output"
    ]
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Dev machine\n"
     ]
    }
   ],
   "source": [
    "import sys\n",
    "import platform\n",
    "if (platform.uname().node == 'TPY14'):\n",
    "    print('Dev machine')\n",
    "    sys.path.append('..\\\\..\\\\..\\\\py4hw')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import py4hw"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "sys = py4hw.HWSystem()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = py4hw.Wire(sys, 'a', 2)\n",
    "b = py4hw.Wire(sys, 'b', 2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Wires are the fundamental way to transport values along circuits. A wire has a single source (circuit) and can have multiple sinks (circuits).\n",
    "\n",
    "Thus, the Wire class implements some of the required simulation logic.\n",
    "\n",
    "\n",
    "Behavioural circuits can check the actual value of a wire connected to an input port and can drive a wire connected to and output port to a certain value.\n",
    "\n",
    "Reading the actual value from a wire **w** is done with the **w.get()** method.\n",
    "Setting the value of a wire is either done with the **w.put( < value > )** or the **w.prepare(< value >)**.\n",
    "\n",
    "Remember that this should only be done inside the implementation of a beharioual circuit, and this is not the intended way to inject signals during simulation.\n",
    "\n",
    "But, why do we have 2 different methods **put** and **prepare** ? \n",
    "\n",
    "Imagine this circuit...\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAckAAAHJCAYAAADuP8JxAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjUuMSwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/YYfK9AAAACXBIWXMAAA9hAAAPYQGoP6dpAAAXMElEQVR4nO3deZSddZ3n8c8v7EuAsKSBEEXAdeLYCjjaOoooLqhjIzouY4Nbg7atrbbRcWhPd6uNaBpx0Jk+tMc57doq4IILKqJgO9rjNjqmXRpiIwHZQ0gCJIHkN3/cKiiKfJPKemt5vc6pU6lbz3PrV/dXue/7PPe592m99wAA9zdr2AMAgMlKJAGgIJIAUBBJACiIJAAURBIACiIJAAWRBICCSAJAQSQBoCCSsAGttd1aax9srd3cWru9tXZRa+2wYY9rJmmtndZau6y1tqK11ltr+w17TMw8IgnjtNZ2TfKBJCcleXGSJybZO8mXW2s7DXFoM8bIHOyZ5GtJzhzycJjBdh72ALal1tozk/xFkgVJ1iX5fpI/670vGerAmNRaa5clWZxkbZJTkixN8u+S/FHv/Zsjy7xs5PKnJfn6cEY6fW1gDv6l9/7kke8dN7SBzTCttZZkYZLXJDkkyb8meVfv/YKhDmyIptuW5F5J3p/k2CRPTbI+yedba9Pt92TbOzXJ3UmekOTDSXZJ8o3Rb/bef5fBnfgfDGV0M8PYOTh9yGOZqd6d5BVJXpvBA8VzknyitfbkoY5qiKbVlmTv/cKxX7fWXpXkxiSPyOAODipX9t7fm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BBJACiIJAAURBIACiIJAAWRBICCSAJAQSQBoCCSAFAQSQAoiCQAFEQSAAoiCQAFkQSAgkgCQEEkAaAgkgBQEEkAKIgkABREEgAKIgkABZEEgIJIAkBBJAGgIJIAUBBJACiIJAAURBIACiIJAAWRBICCSAJAQSQBoCCSAFAQSQAoiCQAFEQSAAoiCQAFkQSAgkgCQEEkAaAgkgBQEEkAKIgkABREEgAKIgkABZEEgIJIAkBBJAGgIJIAUBBJACiIJAAURBIACiIJAAWRBICCSAJAQSQBoCCSAFAQSQAoiCQAFEQSAAoiCQAFkQSAgkgCQEEkAaAgkgBQEEkAKIgkABREEgAKIgkABZEEgIJIAkBBJAGgIJIAUBBJACiIJAAURBIACiIJAAWRBICCSAJAQSQBoCCSAFAQSQAoiCQAFEQSAAoiCQAFkQSAgkgCQEEkAaAgkgBQEEkAKIgkABREEgAKIgkABZEEgIJIAkBBJAGgIJIAUBBJACiIJAAURBIACiIJAAWRBICCSAJAQSQBoCCSAFAQSQAoiCQAFEQSAAoiCQAFkQSAgkgCQEEkAaAgkgBQEEkAKIgkABREEgAKIgkABZEEgIJIAkBBJAGgIJIAUBBJACiIJAAURBIACiIJAAWRBICCSAJAQSQBoCCSAFAQSQAoiCQAFEQSAAoiCQAFkQSAgkgCQEEkAaAgkgBQEEkAKIgkABREEgAKIgkABZEEgIJIAkBBJAGg8P8BNItcZ6la2DMAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<Figure size 565x565 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "py4hw.Sequence(sys, 'a', [0,1,0,0], a)\n",
    "c = py4hw.Wire(sys, 'c', 2)\n",
    "d = py4hw.Wire(sys, 'd', 2)\n",
    "e = py4hw.Wire(sys, 'e', 2)\n",
    "\n",
    "py4hw.Reg(sys, 'r0', d=a, q=d)\n",
    "py4hw.Reg(sys, 'r1', d=d, q=e)\n",
    "\n",
    "py4hw.Scope(sys, 'e', e)\n",
    "sch = py4hw.Schematic(sys)\n",
    "sch.draw()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Cycle based simulators (as used by py4hw) use 2 phases to simulate circuits. \n",
    "In a first phase all the combinational signals are propagated.\n",
    "Then all sequential circuits are evaluated.\n",
    "\n",
    "Combinational circuits are sorted by data dependency as no combinational loops are allowed. Thus, combinational behavioural circuits can call **put()** on wires because values will correctly be propagated.\n",
    "\n",
    "On the other hand, sequential circuits must consider the values of their inputs during the clock edge.\n",
    "If r0 is evalued first and it uses the **put()** method, the input of the r1 circuit will see the value corresponding to the next cycle instead of the current one.\n",
    "\n",
    "To prevent that from happening, sequential circuits MUST use the **prepare()** method. After all sequential circuits are evaluated during a clock cycle, the simulator will transfer the prepared values to the wires. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
